/*   1:    */package com.linkage.bss.commons.util;
/*   2:    */
/*   3:    */import java.io.BufferedReader;
/*   4:    */import java.io.IOException;
/*   5:    */import java.io.StringReader;
/*   6:    */import java.util.Arrays;
/*   7:    */import java.util.Locale;
/*   8:    */import java.util.SortedSet;
/*   9:    */import java.util.TreeSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*  10:    */
/*  19:    */public class JavascriptUtil
/*  20:    */{
/*  21: 21 */ private static Logger log = LoggerFactory.getLogger(JsonUtil.class);
/*  22:    */  
/*  24:    */  public static final int COMPRESS_STRIP_SL_COMMENTS = 1;
/*  25:    */  
/*  27:    */  public static final int COMPRESS_STRIP_ML_COMMENTS = 2;
/*  28:    */  
/*  30:    */  public static final int COMPRESS_TRIM_LINES = 4;
/*  31:    */  
/*  33:    */  public static final int COMPRESS_STRIP_BLANKLINES = 8;
/*  34:    */  
/*  36:    */  public static final int COMPRESS_SHRINK_VARS = 16;
/*  37:    */  
/*  39:    */  public static final int COMPRESS_REMOVE_NEWLINES = 32;
/*  40:    */  
/*  42:    */  public static final int LEVEL_NONE = 0;
/*  43:    */  
/*  45:    */  public static final int LEVEL_DEBUGGABLE = 7;
/*  46:    */  
/*  47:    */  public static final int LEVEL_NORMAL = 31;
/*  48:    */  
/*  49:    */  public static final int LEVEL_ULTRA = 63;
/*  50:    */  
/*  52:    */  public static String compress(String text, int level)
/*  53:    */  {
/*  54: 54 */    String reply = text;
/*  55:    */    
/*  57: 57 */    if ((level & 0x2) != 0)
/*  58:    */    {
/*  59: 59 */      reply = stripMultiLineComments(text);
/*  60:    */    }
/*  61:    */    
/*  62: 62 */    if ((level & 0x1) != 0)
/*  63:    */    {
/*  64: 64 */      reply = stripSingleLineComments(reply);
/*  65:    */    }
/*  66:    */    
/*  67: 67 */    if ((level & 0x4) != 0)
/*  68:    */    {
/*  69: 69 */      reply = trimLines(reply);
/*  70:    */    }
/*  71:    */    
/*  72: 72 */    if ((level & 0x8) != 0)
/*  73:    */    {
/*  74: 74 */      reply = stripBlankLines(reply);
/*  75:    */    }
/*  76:    */    
/*  77: 77 */    if ((level & 0x10) != 0)
/*  78:    */    {
/*  79: 79 */      reply = shrinkVariableNames(reply);
/*  80:    */    }
/*  81:    */    
/*  82: 82 */    if ((level & 0x20) != 0)
/*  83:    */    {
/*  84: 84 */      reply = stripNewlines(reply);
/*  85:    */    }
/*  86:    */    
/*  87: 87 */    return reply;
/*  88:    */  }
/*  89:    */  
/*  98:    */  public static String trimLines(String text)
/*  99:    */  {
/* 100:100 */    if (text == null)
/* 101:    */    {
/* 102:102 */      return null;
/* 103:    */    }
/* 104:    */    
/* 105:    */    try
/* 106:    */    {
/* 107:107 */      StringBuffer output = new StringBuffer();
/* 108:    */      
/* 110:110 */      BufferedReader in = new BufferedReader(new StringReader(text));
/* 111:    */      for (;;)
/* 112:    */      {
/* 113:113 */        String line = in.readLine();
/* 114:114 */        if (line == null) {
/* 115:    */          break;
/* 116:    */        }
/* 117:    */        
/* 119:119 */        output.append(line.trim());
/* 120:120 */        output.append('\n');
/* 121:    */      }
/* 122:    */      
/* 123:123 */      return output.toString();
/* 124:    */    }
/* 125:    */    catch (IOException ex)
/* 126:    */    {
/* 127:127 */      log.error("IOExecption unexpected.", ex);
/* 128:128 */      throw new IllegalArgumentException("IOExecption unexpected.");
/* 129:    */    }
/* 130:    */  }
/* 131:    */  
/* 137:    */  public static String stripSingleLineComments(String text)
/* 138:    */  {
/* 139:139 */    if (text == null)
/* 140:    */    {
/* 141:141 */      return null;
/* 142:    */    }
/* 143:    */    
/* 144:    */    try
/* 145:    */    {
/* 146:146 */      StringBuffer output = new StringBuffer();
/* 147:    */      
/* 148:148 */      BufferedReader in = new BufferedReader(new StringReader(text));
/* 149:    */      for (;;)
/* 150:    */      {
/* 151:151 */        String line = in.readLine();
/* 152:152 */        if (line == null) {
/* 153:    */          break;
/* 154:    */        }
/* 155:    */        
/* 158:158 */        if (line.indexOf("#bss.serviceframework") == -1)
/* 159:    */        {
/* 160:160 */          int cstart = line.indexOf("//");
/* 161:161 */          if (cstart >= 0)
/* 162:    */          {
/* 163:163 */            line = line.substring(0, cstart);
/* 164:    */          }
/* 165:    */        }
/* 166:    */        
/* 167:167 */        output.append(line);
/* 168:168 */        output.append('\n');
/* 169:    */      }
/* 170:    */      
/* 171:171 */      return output.toString();
/* 172:    */    }
/* 173:    */    catch (IOException ex)
/* 174:    */    {
/* 175:175 */      log.error("IOExecption unexpected.", ex);
/* 176:176 */      throw new IllegalArgumentException("IOExecption unexpected.");
/* 177:    */    }
/* 178:    */  }
/* 179:    */  
/* 185:    */  public static String stripMultiLineComments(String text)
/* 186:    */  {
/* 187:187 */    if (text == null)
/* 188:    */    {
/* 189:189 */      return null;
/* 190:    */    }
/* 191:    */    
/* 192:    */    try
/* 193:    */    {
/* 194:194 */      StringBuffer output = new StringBuffer();
/* 195:    */      
/* 203:203 */      boolean inMultiLine = false;
/* 204:204 */      BufferedReader in = new BufferedReader(new StringReader(text));
/* 205:    */      for (;;)
/* 206:    */      {
/* 207:207 */        String line = in.readLine();
/* 208:208 */        if (line == null) {
/* 209:    */          break;
/* 210:    */        }
/* 211:    */        
/* 213:213 */        if (!inMultiLine)
/* 214:    */        {
/* 216:216 */          int cstart = line.indexOf("/*");
/* 217:217 */          if (cstart >= 0)
/* 218:    */          {
/* 220:220 */            int cend = line.indexOf("*/", cstart + "/*".length());
/* 221:221 */            if (cend >= 0)
/* 222:    */            {
/* 225:225 */              line = line.substring(0, cstart) + " " + line.substring(cend + "*/".length());
/* 227:    */            }
/* 228:    */            else
/* 229:    */            {
/* 230:230 */              inMultiLine = true;
/* 231:231 */              line = line.substring(0, cstart) + " ";
/* 233:    */            }
/* 234:    */            
/* 236:    */          }
/* 237:    */          
/* 240:    */        }
/* 241:    */        else
/* 242:    */        {
/* 244:244 */          int cend = line.indexOf("*/");
/* 245:245 */          if (cend >= 0)
/* 246:    */          {
/* 248:248 */            line = line.substring(cend + "*/".length());
/* 249:249 */            inMultiLine = false;
/* 251:    */          }
/* 252:    */          else
/* 253:    */          {
/* 254:254 */            line = " ";
/* 255:    */          }
/* 256:    */        }
/* 257:    */        
/* 258:258 */        output.append(line);
/* 259:259 */        output.append('\n');
/* 260:    */      }
/* 261:    */      
/* 262:262 */      return output.toString();
/* 263:    */    }
/* 264:    */    catch (IOException ex)
/* 265:    */    {
/* 266:266 */      log.error("IOExecption unexpected.", ex);
/* 267:267 */      throw new IllegalArgumentException("IOExecption unexpected.");
/* 268:    */    }
/* 269:    */  }
/* 270:    */  
/* 278:    */  public static String stripBlankLines(String text)
/* 279:    */  {
/* 280:280 */    if (text == null)
/* 281:    */    {
/* 282:282 */      return null;
/* 283:    */    }
/* 284:    */    
/* 285:    */    try
/* 286:    */    {
/* 287:287 */      StringBuffer output = new StringBuffer();
/* 288:    */      
/* 289:289 */      BufferedReader in = new BufferedReader(new StringReader(text));
/* 290:290 */      boolean doneOneLine = false;
/* 291:    */      for (;;)
/* 292:    */      {
/* 293:293 */        String line = in.readLine();
/* 294:294 */        if (line == null) {
/* 295:    */          break;
/* 296:    */        }
/* 297:    */        
/* 299:299 */        if (line.trim().length() > 0)
/* 300:    */        {
/* 301:301 */          output.append(line);
/* 302:302 */          output.append('\n');
/* 303:303 */          doneOneLine = true;
/* 304:    */        }
/* 305:    */      }
/* 306:    */      
/* 307:307 */      if (!doneOneLine)
/* 308:    */      {
/* 309:309 */        output.append('\n');
/* 310:    */      }
/* 311:    */      
/* 312:312 */      return output.toString();
/* 313:    */    }
/* 314:    */    catch (IOException ex)
/* 315:    */    {
/* 316:316 */      log.error("IOExecption unexpected.", ex);
/* 317:317 */      throw new IllegalArgumentException("IOExecption unexpected.");
/* 318:    */    }
/* 319:    */  }
/* 320:    */  
/* 326:    */  public static String stripNewlines(String text)
/* 327:    */  {
/* 328:328 */    if (text == null)
/* 329:    */    {
/* 330:330 */      return null;
/* 331:    */    }
/* 332:    */    
/* 333:    */    try
/* 334:    */    {
/* 335:335 */      StringBuffer output = new StringBuffer();
/* 336:    */      
/* 337:337 */      BufferedReader in = new BufferedReader(new StringReader(text));
/* 338:    */      for (;;)
/* 339:    */      {
/* 340:340 */        String line = in.readLine();
/* 341:341 */        if (line == null) {
/* 342:    */          break;
/* 343:    */        }
/* 344:    */        
/* 346:346 */        output.append(line);
/* 347:347 */        output.append(" ");
/* 348:    */      }
/* 349:349 */      output.append('\n');
/* 350:    */      
/* 351:351 */      return output.toString();
/* 352:    */    }
/* 353:    */    catch (IOException ex)
/* 354:    */    {
/* 355:355 */      log.error("IOExecption unexpected.", ex);
/* 356:356 */      throw new IllegalArgumentException("IOExecption unexpected.");
/* 357:    */    }
/* 358:    */  }
/* 359:    */  
/* 365:    */  public static String shrinkVariableNames(String text)
/* 366:    */  {
/* 367:367 */    if (text == null)
/* 368:    */    {
/* 369:369 */      return null;
/* 370:    */    }
/* 371:371 */    return text;
/* 372:    */  }
/* 373:    */  
/* 397:    */  public static String escapeJavaScript(String str)
/* 398:    */  {
/* 399:399 */    if (str == null)
/* 400:    */    {
/* 401:401 */      return null;
/* 402:    */    }
/* 403:    */    
/* 404:404 */    StringBuffer writer = new StringBuffer(str.length() * 2);
/* 405:    */    
/* 406:406 */    int sz = str.length();
/* 407:407 */    for (int i = 0; i < sz; i++)
/* 408:    */    {
/* 409:409 */      char ch = str.charAt(i);
/* 410:    */      
/* 412:412 */      if (ch > '࿿')
/* 413:    */      {
/* 414:414 */        writer.append("\\u");
/* 415:415 */        writer.append(hex(ch));
/* 416:    */      }
/* 417:417 */      else if (ch > 'ÿ')
/* 418:    */      {
/* 419:419 */        writer.append("\\u0");
/* 420:420 */        writer.append(hex(ch));
/* 421:    */      }
/* 422:422 */      else if (ch > '')
/* 423:    */      {
/* 424:424 */        writer.append("\\u00");
/* 425:425 */        writer.append(hex(ch));
/* 426:    */      }
/* 427:427 */      else if (ch < ' ')
/* 428:    */      {
/* 429:429 */        switch (ch)
/* 430:    */        {
/* 431:    */        case '\b': 
/* 432:432 */          writer.append('\\');
/* 433:433 */          writer.append('b');
/* 434:434 */          break;
/* 435:    */        case '\n': 
/* 436:436 */          writer.append('\\');
/* 437:437 */          writer.append('n');
/* 438:438 */          break;
/* 439:    */        case '\t': 
/* 440:440 */          writer.append('\\');
/* 441:441 */          writer.append('t');
/* 442:442 */          break;
/* 443:    */        case '\f': 
/* 444:444 */          writer.append('\\');
/* 445:445 */          writer.append('f');
/* 446:446 */          break;
/* 447:    */        case '\r': 
/* 448:448 */          writer.append('\\');
/* 449:449 */          writer.append('r');
/* 450:450 */          break;
/* 451:    */        case '\013': default: 
/* 452:452 */          if (ch > '\017')
/* 453:    */          {
/* 454:454 */            writer.append("\\u00");
/* 455:455 */            writer.append(hex(ch));
/* 456:    */          }
/* 457:    */          else
/* 458:    */          {
/* 459:459 */            writer.append("\\u000");
/* 460:460 */            writer.append(hex(ch));
/* 461:    */          }
/* 462:462 */          break;
/* 463:    */        }
/* 464:    */      }
/* 465:    */      else
/* 466:    */      {
/* 467:467 */        switch (ch)
/* 468:    */        {
/* 471:    */        case '\'': 
/* 472:472 */          writer.append('\\');
/* 473:473 */          writer.append('\'');
/* 474:474 */          break;
/* 475:    */        case '"': 
/* 476:476 */          writer.append('\\');
/* 477:477 */          writer.append('"');
/* 478:478 */          break;
/* 479:    */        case '\\': 
/* 480:480 */          writer.append('\\');
/* 481:481 */          writer.append('\\');
/* 482:482 */          break;
/* 483:    */        default: 
/* 484:484 */          writer.append(ch);
/* 485:    */        }
/* 486:    */        
/* 487:    */      }
/* 488:    */    }
/* 489:    */    
/* 490:490 */    return writer.toString();
/* 491:    */  }
/* 492:    */  
/* 499:    */  private static String hex(char ch)
/* 500:    */  {
/* 501:501 */    return Integer.toHexString(ch).toUpperCase(Locale.ENGLISH);
/* 502:    */  }
/* 503:    */  
/* 512:    */  public static String unescapeJavaScript(String str)
/* 513:    */  {
/* 514:514 */    if (str == null)
/* 515:    */    {
/* 516:516 */      return null;
/* 517:    */    }
/* 518:    */    
/* 519:519 */    StringBuffer writer = new StringBuffer(str.length());
/* 520:520 */    int sz = str.length();
/* 521:521 */    StringBuffer unicode = new StringBuffer(4);
/* 522:522 */    boolean hadSlash = false;
/* 523:523 */    boolean inUnicode = false;
/* 524:    */    
/* 525:525 */    for (int i = 0; i < sz; i++)
/* 526:    */    {
/* 527:527 */      char ch = str.charAt(i);
/* 528:528 */      if (inUnicode)
/* 529:    */      {
/* 532:532 */        unicode.append(ch);
/* 533:533 */        if (unicode.length() == 4)
/* 534:    */        {
/* 536:    */          try
/* 537:    */          {
/* 539:539 */            int value = Integer.parseInt(unicode.toString(), 16);
/* 540:540 */            writer.append((char)value);
/* 541:541 */            unicode.setLength(0);
/* 542:542 */            inUnicode = false;
/* 543:543 */            hadSlash = false;
/* 544:    */          }
/* 545:    */          catch (NumberFormatException nfe)
/* 546:    */          {
/* 547:547 */            throw new IllegalArgumentException("Unable to parse unicode value: " + unicode + " cause: " + nfe);
/* 548:    */          }
/* 549:    */          
/* 550:    */        }
/* 551:    */        
/* 552:    */      }
/* 553:553 */      else if (hadSlash)
/* 554:    */      {
/* 556:556 */        hadSlash = false;
/* 557:557 */        switch (ch)
/* 558:    */        {
/* 559:    */        case '\\': 
/* 560:560 */          writer.append('\\');
/* 561:561 */          break;
/* 562:    */        case '\'': 
/* 563:563 */          writer.append('\'');
/* 564:564 */          break;
/* 565:    */        case '"': 
/* 566:566 */          writer.append('"');
/* 567:567 */          break;
/* 568:    */        case 'r': 
/* 569:569 */          writer.append('\r');
/* 570:570 */          break;
/* 571:    */        case 'f': 
/* 572:572 */          writer.append('\f');
/* 573:573 */          break;
/* 574:    */        case 't': 
/* 575:575 */          writer.append('\t');
/* 576:576 */          break;
/* 577:    */        case 'n': 
/* 578:578 */          writer.append('\n');
/* 579:579 */          break;
/* 580:    */        case 'b': 
/* 581:581 */          writer.append('\b');
/* 582:582 */          break;
/* 583:    */        
/* 584:    */        case 'u': 
/* 585:585 */          inUnicode = true;
/* 586:586 */          break;
/* 587:    */        default: 
/* 588:588 */          writer.append(ch);
/* 589:    */          
/* 591:591 */          break;
/* 592:    */        }
/* 593:593 */      } else if (ch == '\\')
/* 594:    */      {
/* 595:595 */        hadSlash = true;
/* 596:    */      }
/* 597:    */      else {
/* 598:598 */        writer.append(ch);
/* 599:    */      }
/* 600:    */    }
/* 601:601 */    if (hadSlash)
/* 602:    */    {
/* 605:605 */      writer.append('\\');
/* 606:    */    }
/* 607:    */    
/* 608:608 */    return writer.toString();
/* 609:    */  }
/* 610:    */  
/* 617:    */  public static boolean isReservedWord(String name)
/* 618:    */  {
/* 619:619 */    return reserved.contains(name);
/* 620:    */  }
/* 621:    */  
/* 626:626 */  private static final String[] RESERVED_ARRAY = {
/* 627:    */  
/* 628:628 */    "as", 
/* 629:629 */    "break", 
/* 630:630 */    "case", 
/* 631:631 */    "catch", 
/* 632:632 */    "class", 
/* 633:633 */    "const", 
/* 634:634 */    "continue", 
/* 635:635 */    "default", 
/* 636:636 */    "delete", 
/* 637:637 */    "do", 
/* 638:638 */    "else", 
/* 639:639 */    "export", 
/* 640:640 */    "extends", 
/* 641:641 */    "false", 
/* 642:642 */    "finally", 
/* 643:643 */    "for", 
/* 644:644 */    "function", 
/* 645:645 */    "if", 
/* 646:646 */    "import", 
/* 647:647 */    "in", 
/* 648:648 */    "instanceof", 
/* 649:649 */    "is", 
/* 650:650 */    "namespace", 
/* 651:651 */    "new", 
/* 652:652 */    "null", 
/* 653:653 */    "package", 
/* 654:654 */    "private", 
/* 655:655 */    "public", 
/* 656:656 */    "return", 
/* 657:657 */    "super", 
/* 658:658 */    "switch", 
/* 659:659 */    "this", 
/* 660:660 */    "throw", 
/* 661:661 */    "true", 
/* 662:662 */    "try", 
/* 663:663 */    "typeof", 
/* 664:664 */    "use", 
/* 665:665 */    "var", 
/* 666:666 */    "void", 
/* 667:667 */    "while", 
/* 668:668 */    "with", 
/* 669:    */    
/* 670:670 */    "abstract", 
/* 671:671 */    "debugger", 
/* 672:672 */    "enum", 
/* 673:673 */    "goto", 
/* 674:674 */    "implements", 
/* 675:675 */    "interface", 
/* 676:676 */    "native", 
/* 677:677 */    "protected", 
/* 678:678 */    "synchronized", 
/* 679:679 */    "throws", 
/* 680:680 */    "transient", 
/* 681:681 */    "volatile", 
/* 682:    */    
/* 683:683 */    "boolean", 
/* 684:684 */    "byte", 
/* 685:685 */    "char", 
/* 686:686 */    "double", 
/* 687:687 */    "final", 
/* 688:688 */    "float", 
/* 689:689 */    "int", 
/* 690:690 */    "long", 
/* 691:691 */    "short", 
/* 692:692 */    "static" };
/* 693:    */  
/* 720:720 */  private static SortedSet reserved = new TreeSet();
/* 721:    */  private static final String SPACE = " ";
/* 722:    */  private static final String COMMENT_ML_START = "/*";
/* 723:    */  private static final String COMMENT_ML_END = "*/";
/* 724:    */  private static final String COMMENT_SL_START = "//";
/* 725:    */  private static final String COMMENT_RETAIN = "#bss.serviceframework";
/* 726:    */  
/* 727:    */  static {
/* 728:728 */    reserved.addAll(Arrays.asList(RESERVED_ARRAY));
/* 729:    */  }
/* 730:    */}


/* Location:           E:\workspace\sungl111\workspace_promotion\BizHallConfig\WebRoot\WEB-INF\lib\bss.commons-0.1.jar
 * Qualified Name:     com.linkage.bss.commons.util.JavascriptUtil
 * JD-Core Version:    0.7.0-SNAPSHOT-20130630
 */